<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/11 0011
 * Time: 下午 5:41
 */
namespace Admin\Model;
class AuthGroupAccessModel{

    public function set_group_auth_access($data){
        $data['uid'] = $data['a_id'];
        if(empty($data['a_id'])){
            $error="用户id不能为空！！";
        }elseif(!M('AuthGroup')->getById(trim($data['group_id']))){
            $error="该角色不存在";
        }else{
            $sql="select * from ".C('DB_PREFIX')."auth_group_access where uid = ".$data['a_id'];
            $groupAccess = M()->query($sql);
            if($groupAccess && !empty($groupAccess)){
                $r=M('AuthGroupAccess')->where(array('uid'=>$data['a_id']))->save($data);
                if($r){
                    return array(
                        'status'=>'success',
                        'message'=>'修改成功',
                        'url'=>'AuthGroup/index'
                    );
                }else{
                    return array(
                        'status'=>'success',
                        'message'=>'修改失败',
                        'url'=>'AuthGroup/index'
                    );
                }

            }
        }

        if($error){
            return array(
                'status'=>'back',
                'message'=>$error
            );
        }
//        $data['status']  =$data['status'];


        if(M('AuthGroupAccess')->add($data)){

            return array(
                'status'=>'success',
                'message'=>'新增成功',
                'url'=>'AuthGroup/index'
            );
        }else{
            return array(
                'status'=>'back',
                'message'=>'新增失败'

            );
        }
    }

    public function show_Auth_group_page(){
        $sql="select * from ".C('DB_PREFIX')."auth_group ";

        $count=count(M()->query($sql));
        $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
        $show  = $Page->show();// 分页显示输出

        $sql.=' order by id desc LIMIT '.$Page->firstRow.','.$Page->listRows;

        $list=M()->query($sql);

        return array(
            'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
            'list'=>$list,
            'page'=>$show
        );

    }

    public function add_auth_group($data){

        if(empty($data['title'])){
            $error="角色名不能为空！！";
        }elseif(M('AuthGroup')->getByTitle(trim($data['title']))){
            $error="该角色名称以及存在！！";
        }

        if($error){
            return array(
                'status'=>'back',
                'message'=>$error
            );
        }
//        $data['status']  =$data['status'];


        if(M('AuthGroup')->add($data)){

            return array(
                'status'=>'success',
                'message'=>'新增成功',
                'url'=>'AuthGroup/index'
            );
        }else{
            return array(
                'status'=>'back',
                'message'=>'新增失败'

            );
        }
    }
    public function findAllAuthGroup(){
        $sql="select * from ".C('DB_PREFIX')."auth_group "."where status = 1";
        $list=M()->query($sql);

        return array(
            'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
            'list'=>$list,
        );

    }
    public function edit_auth_group($data){
        $r=M('AuthGroup')->where(array('id'=>$data['id']))->save($data);
        if($r){
            return array(
                'status'=>'success',
                'message'=>'修改成功',
                'url'=>'AuthGroup/index'
            );
        }else{
            return array(
                'status'=>'fail',
                'message'=>'修改失败',
                'url'=>'AuthGroup/index'
            );
        }
    }

}